package meicai.appium;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

class EchartsData {
	public EchartsData() {
		super();
	}
	public int getMonthCapacity() {
		return monthCapacity;
	}
	public void setMonthCapacity(int monthCapacity) {
		this.monthCapacity = monthCapacity;
	}
	public int getRequestTotal() {
		return requestTotal;
	}
	public void setRequestTotal(int requestTotal) {
		this.requestTotal = requestTotal;
	}
	public int getCustomerOrder() {
		return customerOrder;
	}
	public void setCustomerOrder(int customerOrder) {
		this.customerOrder = customerOrder;
	}
	public int getSelfOrder() {
		return selfOrder;
	}
	public void setSelfOrder(int selfOrder) {
		this.selfOrder = selfOrder;
	}
	public int getSurplus() {
		return surplus;
	}
	public void setSurplus(int surplus) {
		this.surplus = surplus;
	}
	public int getExceptionOrder() {
		return exceptionOrder;
	}
	public void setExceptionOrder(int exceptionOrder) {
		this.exceptionOrder = exceptionOrder;
	}
	public int getSendOrderInToday() {
		return sendOrderInToday;
	}
	public void setSendOrderInToday(int sendOrderInToday) {
		this.sendOrderInToday = sendOrderInToday;
	}
//	public int getReduce() {
//		return reduce;
//	}
//	public void setReduce(int reduce) {
//		this.reduce = reduce;
//	}
	public int getDeliverInMonth() {
		return deliverInMonth;
	}
	public void setDeliverInMonth(int deliverInMonth) {
		this.deliverInMonth = deliverInMonth;
	}
	int monthCapacity = 10000; //产能
	int requestTotal;  //需求总量
	int customerOrder; //客订订单
	int selfOrder;     //自订订单
	int surplus;       //剩余产能
	int exceptionOrder;//异常订单
	int sendOrderInToday;//今日发单量
//	int reduce;          //减库存量
	int deliverInMonth;  //预计本月交货
};

/**
 * Hello world!
 * .activity.MainActivity_
 *
 */
public class App 
{
	public void sayHello(String message) {
		System.out.println(message);
	}
	
	public void testPostRequest() {
		JSONObject o = new JSONObject();
    	o.put("query_fsDate", "2018-09-01");
    	o.put("query_feDate", "2018-09-21");
    	o.put("unionNo", "0001");
    	o.put("sysuserid", "10000392");
    	o.put("page", "1");
    	o.put("limit", "50");
    	
    	JSONObject testJson = HttpRequestUtils.httpPost("https://rsm.xuping.cn/appinfo/IpOrdRptList_AppBase.gx", o);
    	//System.out.println("ghd ================ " + r.toJSONString());
    	
    	//厂商//产品名称
    	Map<String,Map<String,EchartsData>> finalMap = new HashMap<String,Map<String,EchartsData>>();
    	
    	//String testJsonStr = "{\"totalCount\":943,\"status\":1,\"data\":[{\"faccessories3\":\"\",\"fgdEmpNo\":\"10000002\",\"faccessories2\":\"\",\"fqty\":10.0,\"fywEmpName\":\"管理员2\",\"fcustomBillNo\":\"\",\"fremark\":\"\",\"faccessories1\":\"\",\"fisExistTemplet\":\"无\",\"fdeptName\":\"00201\",\"fstatu\":\"有效\",\"ftoUnionName\":\"广东旭平首饰\",\"subscription\":0.0,\"fmarkPrice\":22.0,\"fitemName\":\"耳环+圆形+白色+工艺:(无)+电镀:(J4#R)+均码\",\"fspecialPrice\":0.0,\"fgdEmpName\":\"管理员2\",\"fpicPath\":\"/upload/product/20180713/20180713144504_7454657.png\",\"fvendorname\":\"多达\",\"fitemTypeName\":\"成品\",\"fbillTypeName\":\"自订订单\",\"fmouldItemNo\":\"\",\"fbillNo\":\"000000\",\"fremark1\":\"\",\"fzdEmpNo\":\"10000052\",\"fcustname\":\"catherina\",\"fremark2\":\"\",\"fdate\":\"2018-8-3\",\"fitemNo\":\"10432200\",\"freqJhDate\":\"2018-8-4\",\"ffroUnionName\":\"广东旭平首饰\",\"fzdEmpName\":\"苏国权\"}," 
    	//                                                              + "{\"faccessories3\":\"\",\"fplanday\":\"\",\"fgdEmpNo\":\"10002571\",\"faccessories2\":\"\",\"fqty\":700.0,\"fywEmpName\":\"陈冬群\",\"fremark\":\"\",\"faccessories1\":\"\",\"fisExistTemplet\":\"无\",\"fdeptName\":\"00201\",\"fstatu\":\"有效\",\"ftoUnionName\":\"广东旭平首饰\",\"subscription\":1000.0,\"fmarkPrice\":28.0,\"fitemName\":\"吊坠+花式石+4745+10mm+001VL+工艺:(无)+电镀:（J1#R）\",\"fspecialPrice\":0.0,\"fgdEmpName\":\"陈冬群\",\"fpackWayName\":\"A\",\"fpicPath\":\"/upload/product/20180531/20180531192506_2536270.png\",\"fvendorname\":\"多达\",\"fitemTypeName\":\"成品\",\"fbillTypeName\":\"客订订单\",\"fmouldItemNo\":\"\",\"fbillNo\":\"00401020-18\",\"fremark1\":\"精加工\",\"fzdEmpNo\":\"10002571\",\"fcustname\":\"胡珊丽\",\"fremark2\":\"\",\"fdate\":\"2018-8-3\",\"fitemNo\":\"610424464\",\"freqJhDate\":\"2018-8-20\",\"ffroUnionName\":\"广东旭平首饰\",\"fzdEmpName\":\"陈冬群\"}]}";
    	
    	//JSONObject testJson = JSON.parseObject(testJsonStr);
    	JSONArray dataArray = testJson.getJSONArray("data");
    	System.out.println("size =====" + dataArray.size());
    	for(int i = 0; i < dataArray.size(); i++) {
    		System.out.println("i ====== " + i);
    		JSONObject dataJson = dataArray.getJSONObject(i);
    		String vendorName = dataJson.getString("fvendorname");
    		System.out.println("vendorName "+ vendorName);
    		
    		if(dataJson.getString("fstatu").equals("已作废")) {
    			System.out.println("已作废");
    			continue;
    		}
    		
    		if(null == vendorName) {
    			vendorName = "无供应商";
    		}
    		
    		if(vendorName.equals("011") || 
    				vendorName.equals("012") ||
    				vendorName.equals("013") ||
    				vendorName.equals("003") ||
    				vendorName.equals("2001") ||
    				vendorName.equals("组装")) {
    			
    		} else {
    			continue;
    		}
    		

    		
    		if(finalMap.containsKey(vendorName)) {
    			Map<String, EchartsData> productMap = finalMap.get(vendorName);
    			String productName = dataJson.getString("fitemName").split("[+]", 3)[0];
    			if(productMap.containsKey(productName)) {
    				EchartsData edi = (EchartsData)productMap.get(productName);
    				edi.setRequestTotal(edi.getRequestTotal() + dataJson.getIntValue("fqty"));
    				
    				if(dataJson.getString("fbillTypeName").equals("自订订单")) {
    					edi.setSelfOrder(edi.getSelfOrder() + dataJson.getIntValue("fqty"));
    				} else if(dataJson.getString("fbillTypeName").equals("客订订单")) {
    					edi.setCustomerOrder(edi.getCustomerOrder() + dataJson.getIntValue("fqty"));
    				} else {
    					System.out.println("订单类型不支持");
    				}
    				
    				Date date = new Date();
    				SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d");
    				String dateStr = df.format(date);
    				
    				if(dataJson.getString("fdate").equals(dateStr)) {
    					edi.setSendOrderInToday(edi.getSendOrderInToday() + dataJson.getIntValue("fqty"));
    				}
    				
    				df = new SimpleDateFormat("M");
    				String month = df.format(date);
    				System.out.println("mmmmmmmmmmmmmmmmmmmm " + month);
    				System.out.println(dataJson.getString("freqJhDate").split("-",3)[1]);
    				if(dataJson.getString("freqJhDate").split("-",3)[1].equals(month)) {
    					edi.setDeliverInMonth(edi.getDeliverInMonth() + dataJson.getIntValue("fqty"));
    				}
    				
    				productMap.replace(productName, edi);
    			} else {
    				EchartsData ed = new EchartsData();
    				ed.setRequestTotal(ed.getRequestTotal() + dataJson.getIntValue("fqty"));
    				
    				if(dataJson.getString("fbillTypeName").equals("自订订单")) {
    					ed.setSelfOrder(ed.getSelfOrder() + dataJson.getIntValue("fqty"));
    				} else if(dataJson.getString("fbillTypeName").equals("客订订单")) {
    					ed.setCustomerOrder(ed.getCustomerOrder() + dataJson.getIntValue("fqty"));
    				} else {
    					System.out.println("订单类型不支持");
    				}
    				
    				Date date = new Date();
    				SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d");
    				String dateStr = df.format(date);
    				
    				if(dataJson.getString("fdate").equals(dateStr)) {
    					ed.setSendOrderInToday(ed.getSendOrderInToday() + dataJson.getIntValue("fqty"));
    				}
    				
    				df = new SimpleDateFormat("M");
    				String month = df.format(date);
    				System.out.println("mmmmmmmmmmmmmmmmmmmm " + month);
    				System.out.println(dataJson.getString("freqJhDate").split("-",3)[1]);
    				if(dataJson.getString("freqJhDate").split("-",3)[1].equals(month)) {
    					ed.setDeliverInMonth(ed.getDeliverInMonth() + dataJson.getIntValue("fqty"));
    				}
    				
    				productMap.put(productName, ed);
    			}
    			
    		} else {
    			EchartsData ed = new EchartsData();
    			Map<String, EchartsData> productMap = new HashMap<String, EchartsData>();
    			
    			String productName = dataJson.getString("fitemName").split("[+]", 3)[0];
    			
				//EchartsData ed = new EchartsData();
				ed.setRequestTotal(ed.getRequestTotal() + dataJson.getIntValue("fqty"));
				
				if(dataJson.getString("fbillTypeName").equals("自订订单")) {
					ed.setSelfOrder(ed.getSelfOrder() + dataJson.getIntValue("fqty"));
				} else if(dataJson.getString("fbillTypeName").equals("客订订单")) {
					ed.setCustomerOrder(ed.getCustomerOrder() + dataJson.getIntValue("fqty"));
				} else {
					System.out.println("订单类型不支持");
				}
				
				Date date = new Date();
				SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d");
				String dateStr = df.format(date);
				
				if(dataJson.getString("fdate").equals(dateStr)) {
					ed.setSendOrderInToday(ed.getSendOrderInToday() + dataJson.getIntValue("fqty"));
				}
				
				df = new SimpleDateFormat("M");
				String month = df.format(date);
				System.out.println("mmmmmmmmmmmmmmmmmmmm " + month);
				System.out.println(dataJson.getString("freqJhDate").split("-",3)[1]);
				if(dataJson.getString("freqJhDate").split("-",3)[1].equals(month)) {
					ed.setDeliverInMonth(ed.getDeliverInMonth() + dataJson.getIntValue("fqty"));
				}
    				
    			productMap.put(productName, ed);
    			
    			finalMap.put(vendorName, productMap);
    		}
    		
    	}  	
    	
    	//需求总量
    	//客订订单
    	//自订订单
    	//今日发单量
    	//预计本月交货
    	// 按供应商，产品维度汇总
//    	Set<String> set = finalMap.keySet();
//    	for (String key : set) {
//    		System.out.println("供应商 ：  " + key);
//    		Map m = finalMap.get(key);
//    		Set<String> s = m.keySet();
//    		
//    		for(String k : s) {
//    			System.out.println("商品：" + k);
//    			EchartsData e = (EchartsData) m.get(k);
//    			
//    			System.out.println("需求总量 " + "客订单 " + "自订单 " + "今日发单量 " + "预计本月交货 ");
//    			System.out.println( e.getRequestTotal() +"   " + e.getCustomerOrder()
//    			+ "    " + e.getSelfOrder() + "     " + e.getSendOrderInToday() + "     " +
//    			e.getDeliverInMonth());
//    		}
//    		System.out.println("*********************************");
//    	}
    	
    	JSONObject option = new JSONObject();
    	option.put("title", "test title");
    	JSONArray legendArray = new JSONArray();
    	JSONArray seriesArray = new JSONArray();
    	
    	JSONArray dataVArray = new JSONArray();
    	int sumOfSendOrderInToday = 0;
    	
    	//收敛到按供应商汇总    	
    	Set<String> set = finalMap.keySet();
    	legendArray.addAll(set);
    	for (String key : set) {
    		System.out.println("供应商 ：  " + key);
    		Map m = finalMap.get(key);
    		Set<String> s = m.keySet();
    		EchartsData ge = new EchartsData();
    		System.out.println("需求总量 " + "客订单 " + "自订单 " + "今日发单量 " + "预计本月交货 ");
    		for(String k : s) {   			
    			EchartsData e = (EchartsData) m.get(k);   			
    			ge.setRequestTotal(ge.getRequestTotal() + e.getRequestTotal());
    			ge.setCustomerOrder(ge.getCustomerOrder() + e.getCustomerOrder());
    			ge.setSelfOrder(ge.getSelfOrder() + e.getSelfOrder());
    			ge.setSendOrderInToday(ge.getSendOrderInToday() + e.getSendOrderInToday());
    			ge.setDeliverInMonth(ge.getDeliverInMonth() + e.getDeliverInMonth());			
    		}
    		
    		JSONObject seriesItem = new JSONObject();
    		
    		JSONArray dataArray1 = new JSONArray();
    		dataArray1.add(ge.getRequestTotal());
    		dataArray1.add(ge.getCustomerOrder());
    		dataArray1.add(ge.getSelfOrder());
    		dataArray1.add(ge.getSendOrderInToday());
    		dataArray1.add(ge.getDeliverInMonth());
    		
    		seriesItem.put("name", key);
    		seriesItem.put("type","bar");
    		seriesItem.put("data", dataArray1);
    		
    		seriesArray.add(seriesItem);
    		
			System.out.println( ge.getRequestTotal() +"   " + ge.getCustomerOrder()
			+ "    " + ge.getSelfOrder() + "     " + ge.getSendOrderInToday() + "     " +
			ge.getDeliverInMonth());
			
			sumOfSendOrderInToday += ge.getSendOrderInToday();
			
			//datav
			JSONObject dataVItem = new JSONObject();
			dataVItem.put("x", key);
			dataVItem.put("y", ge.getSendOrderInToday());
			dataVItem.put("s", "1");
			dataVArray.add(dataVItem);
			dataVItem = new JSONObject();
			dataVItem.put("x", key);
			dataVItem.put("y", "10000");
			dataVItem.put("s", "2");
			dataVArray.add(dataVItem);
			dataVItem = new JSONObject();
			dataVItem.put("x", key);
			dataVItem.put("y", "0");
			dataVItem.put("s", "3");
			
			dataVArray.add(dataVItem);

    		System.out.println("*********************************");
    	}
    	option.put("legend", legendArray);
    	option.put("series", seriesArray);
    	System.out.println(option.toJSONString());
    	
    	System.out.println(dataVArray.toJSONString());
    	System.out.println(sumOfSendOrderInToday + "");
    	
    	//{"legend":["011","012","013","003","2001","组装"],"series":[{"data":[416405,120036,296369,0,7698],"name":"011","type":"bar"},{"data":[353772,132000,221772,0,17947],"name":"012","type":"bar"},{"data":[747287,129489,617798,0,137700],"name":"013","type":"bar"},{"data":[162749,66780,95969,0,10684],"name":"003","type":"bar"},{"data":[132592,53562,79030,0,27422],"name":"2001","type":"bar"},{"data":[66440,27640,38800,0,1000],"name":"组装","type":"bar"}],"title":"test title"}
    	
	}
	
	
	
    public static void main( String[] args ) {
    	new App().testPostRequest();
    	
//        String test = "";
//         if( !test.equals("")) {
//        	 System.out.println("here");
//         }
//         
//         try {
//			Object  app = Class.forName(App.class.getName()).newInstance();
//			try {
//				Method m = app.getClass().getMethod("sayHello", String.class);
//				try {
//					m.invoke(app, "me");
//				} catch (IllegalArgumentException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (InvocationTargetException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			} catch (NoSuchMethodException | SecurityException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}   	
    	
   
    }
}
